<script src='<?php echo DIR_JS?>ui.datepicker.js' type='text/javascript' language='javascript'> </script>
<div class="section" id="sitemaplist">
  <div class="section-title">Giao báo</div>
  <div class="section-content padding1">
    <form name="frm" id="frm" action="<?php echo $action?>" method="post" enctype="multipart/form-data">
    <input type="hidden" id="diachi" name="diachi" value="" class="text" />
      <div class="button right">
        
        <input type="hidden" name="khachhangid" value="<?php echo $item['khachhangid']?>">
        <input type="hidden" name="masokhachhang" value="<?php echo $item['masokhachhang'] ?>" />
       	<input type="hidden" name="nhanviengiaoid" id="nhanviengiaoid" value="<?php echo $nhanviengiao ?>" />
      </div>
      <div class="clearer">^&nbsp;</div>
      <div id="error" class="error" style="display:none"></div>
      <div>
      	<div id="ben-search">
        	<script language="javascript">
						$(function() {
							$("#tungay").datepicker({
									changeMonth: true,
									changeYear: true,
									dateFormat: 'dd-mm-yy'
									});
							$("#denngay").datepicker({
									changeMonth: true,
									changeYear: true,
									dateFormat: 'dd-mm-yy'
									});
							});
			</script>
            <table>
            	<tr class="left">
                	<td>
                        <label>Ngày phát hành: từ</label>
                        <input type="text" id="tungay" name="tungay" class="text" />
                        <label>đến</label>
                        <input type="text" id="denngay" name="denngay" class="text" />
            		</td>
                </tr>
                <tr class="clearer"><td></td></tr>
                <tr class="left">
                    <td>
                        <label>Khu vực:</label>
                        <select name="khuvucid" id="khuvucid">
                                    <option value="">----Chọn tất cả----</option>
                                    <?php 
                                        foreach($dskhuvuc as $khuvuc)
                                        { 
                                    ?>
                                    <option value="<?php echo $khuvuc['khuvucid']?>">
                                            <?php echo $this->string->getPrefix("&nbsp;&nbsp;&nbsp;&nbsp;",$khuvuc['level'])?>
                                            <?php echo $khuvuc['tenkhuvuc']?>
                                    </option>
                                    <?php 
                                        }
                                    ?>
                        </select>
                                </td>
                                <td>
                        <label>Khách hàng:</label>
                        <select id="khachhangid" name="khachhangid">
                                
                                <?php echo $data_cbkhachhang?>
                        </select>
              		</td>
                    <td>
                        <a class="button-other search" onClick="searchForm()">Tìm kiếm</a>
                        <a class="button all" href="?route=deliman/giaobao">Xem tất cả</a>
                	</td>
            	</tr>
        	</table>
        </div>
        <br />
        <table class="data-table" width="100%">
        	<thead>
            	 <tr>
                    <th>STT</th>                   
                    <th>Khách hàng</th>
                    <th>Địa chỉ</th>
                    <th>Tên kỳ báo</th>
                    <th>Ngày phát hành</th>
                    <th>Số lượng đặt</th>
                    <th>Giá bán</th>
                    <th>Thời gian giao</th>
                    <th>Số lượng giao</th>
                    <th>Ghi chú khách hàng</th>
                    <th>Công cụ</th>                            
                </tr>
            </thead>      
            <?php
            	$index = 1;
            	foreach($datas as $item)
                {
            ?>
            <tbody>
            
            	<tr>
                	<td><?php echo $index++?></td>
                    <td><?php echo $item['hoten'] ?></td>
                    <td><?php echo $item['diachi'] ?></td>
                    <td><?php echo $item['tenkybao']?></td>
                    <td><?php echo $this->date->formatMySQLDate($item['ngayphathanh']) ?></td>
                    <td><?php echo $this->string->numberFormate($item['soluongdat']) ?></td>
                    <td><?php echo $this->string->numberFormate($item['giaban']) ?></td>
                    <td id="ngaygiao<?php echo $item['id']?>">
                    	<?php 
                        	if($item['trangthai'] =="dagiao")
                            { 
                            	echo $this->date->formatMySQLDate($item['ngaygiao'],"longdate");
                            }
                        ?>
                    </td>
                    <!--Gán số lượng đặt vào text box số lượng đã giao-->
                    <?php
                    	if($item['soluongdagiao'] == 0)
                        {
                            $item['soluongdagiao'] = $item['soluongdat'];
                        }
                        
                        $cls = "";
                        if($item['trangthai']=="dagiao")
                        {
                        	$cls = "disabled";
                        }
                        else
                        {
                        	$result = 0;
                        
                            foreach($maxdates as $maxdate)
                            {
                                if($maxdate['tenloaibao']==$item['tenloaibao'] && 
                                        $maxdate['ngayphathanh'] == $item['ngayphathanh'])
                                {
                                    $result++;
                                }
                            }
                        $today = $this->date->formatViewDate($this->date->formatMySQLDate($this->date->getToday()));
                        if($item['trangthai'] =="dagiao" || $result == 0 && $item['ngayphathanh'] < $today)
                        {
                        	$cls = "disabled";
                        }
                        
                        }
                    ?>
                    <td>
                    <input type="text" class="text number" name="soluongdagiao" id="soluongdagiao<?php echo $item['id']?>" value="<?php echo $item['soluongdagiao']?>" <?php echo $cls ?> />
                    </td>
       
                    <td>                 
                   <textarea  cols="20" rows="3" <?php echo $cls ?> id="ghichu<?php echo $item['id']?>"><?php echo $item['comment'];?></textarea>                    	
                    </td>
                    
                    <td>                
                    <input type="button" class="button confirm <?php echo $cls ?> btnConfirm" ref="<?php echo $item['id'] ?>"  value="Xác nhận giao" name="btnConfirm" id="btnConfirm<?php echo $item['id']?>" <?php echo $cls ?> onClick="update" />
                    </td>
                    
                </tr>
            </tbody>
            <?php
            	}
            ?>
        </table>
      </div>
    </form>
  </div>
</div>

<script language="javascript">
$(document).ready(function(){
		$("#khuvucid").change(function(){
			$('#khachhangid').load('?route=deliman/giaobao/getkhachhang&khuvucid='+$('#khuvucid').val(), function(){
				$("#khachhangid").val("<?php echo $_GET['khachhangid']?>");
			});
			
		});
		$("#khuvucid").change();
		
		
		
		$('.btnConfirm').click(function(){
			//alert($(this).attr('ref'));
			//alert($('#nhanviengiaoid').val());
			//Gui request len sever
			$.post("?route=deliman/giaobao/updatethongtindatbao", 
			{
				id:$(this).attr('ref'),
				ghichu:$('#ghichu'+$(this).attr('ref')).val(),
				soluongdagiao:$('#soluongdagiao' + $(this).attr('ref')).val(),
				//Cap nhat lai khi co phan login nhan vien
				nhanviengiaoid:$('#nhanviengiaoid').val()
			},
			
			function(data){
				//Hien thi ngay server tra ve
				//alert(data);
				obj = jQuery.parseJSON(data);
				if(obj.error == '')
				{
					id=obj.id;
					ngaygiao = obj.ngaygiao;
					soluongdagiao = obj.soluongdagiao;
					$('#ngaygiao'+id).html(ngaygiao);
					$('#soluongdagiao'+id).val(soluongdagiao);
					//Chuyen text box nhap lieu -> readonly va an nut confirm	
					$('#ghichu'+id).attr('readonly','readonly');
					$('#btnConfirm'+id).attr('disabled','disabled');
					$(this).hide();
					window.location.reload();
				}
				else
				{
					alert(obj.error);	
				}
			});	
		});
				
});

/*function save()
{
	$.blockUI({ message: "<h1>Please wait...</h1>" }); 
		
	$.post("?route=deliman/khachhang/savethongtindatbao", $("#frm").serialize(),
		function(data){
			if(data == "true")
			{
				window.location = "?route=deliman/khachhang";
				
			}
			else
			{
			
				$('#error').html(data).show('slow');
				$.unblockUI();
				
			}
			
		}
	);
}*/

function update()
{
	$.blockUI({ message: "<h1>Please wait...</h1>" });
	$.post("?route=deliman/giaobao/updatethongtindatbao", $("#frm").serialize(),
		function(data){
			if(data == "true")
			{
				
				window.location = "?route=deliman/khachhang";
				
			}
			else
			{
			
				$('#error').html(data).show('slow');
				$.unblockUI();
				
			}
			
		}
	);
}

function searchForm()
{
	var url ="?route=deliman/giaobao";
		
	if($("#tungay").val() != "")
		url += "&tungay="+ $("#tungay").val();
	if($("#denngay").val() != "")
		url += "&denngay=" + $("#denngay").val();
	if($("#khuvucid").val() != "")
		url += "&khuvucid=" + $("#khuvucid").val();
	if($("#nhanvienid").val() != "")
		url += "&khachhangid=" + $("#khachhangid").val();
	
	window.location = url;
	$("#khachhangid").val("<?php echo $_GET['khachhangid']?>");
}

$("#tungay").val("<?php echo $_GET['tungay']?>");
$("#denngay").val("<?php echo $_GET['denngay']?>");
$("#khachhangid").val("<?php echo $_GET['khachhangid']?>");
$("#khuvucid").val("<?php echo $_GET['khuvucid']?>");
</script>